package com.szp.spCloud.user.filter;

import com.szp.spCloud.common.service.TokenValidateService;
import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpCookie;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.List;
@Component
public class TokenFilter implements GlobalFilter, Ordered {
    Logger logger = LoggerFactory.getLogger(TokenFilter.class);
    @Reference
    TokenValidateService tokenValidateService;
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 思路：获取客户端ip，判断是否在⿊名单中，在的话就拒绝访问，不在的话就放⾏
        // 从上下⽂中取出request和response对象
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        //放行/api/user/* ,/api/code/* ,/api/email/* 开头的请求
        String url = request.getURI().getPath();
        if((url.indexOf("/api/user") != -1 || url.indexOf("/api/code") != -1) && (url.indexOf("/api/user/info") == -1)){
            return chain.filter(exchange);
        }
// 从request中获取cookie
        MultiValueMap<String, HttpCookie> cookies = request.getCookies();
        List<HttpCookie> httpCookies = cookies.get("token");
        String token = null;
        if(null == httpCookies){
            response.setStatusCode(HttpStatus.SEE_OTHER);
            DataBuffer wrap = response.bufferFactory().wrap("not login user/未授权的访问".getBytes());
            return response.writeWith(Mono.just(wrap));
        }
        for (HttpCookie httpCookie : httpCookies) {
            String name = httpCookie.getName();
            if ("token".equals(name))
                token = httpCookie.getValue();
        }
        if(null == token){
            response.setStatusCode(HttpStatus.SEE_OTHER);
            DataBuffer wrap = response.bufferFactory().wrap("not login user/未授权的访问".getBytes());
            return response.writeWith(Mono.just(wrap));
        }

        String email = tokenValidateService.info(token);
        if(null == email){
            response.setStatusCode(HttpStatus.SEE_OTHER);
            DataBuffer wrap = response.bufferFactory().wrap("not login user".getBytes());
            return response.writeWith(Mono.just(wrap));
        }
        DataBuffer wrap = response.bufferFactory().wrap(email.getBytes());
        return response.writeWith(Mono.just(wrap));
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
